using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using VideoSource;
using System.Windows.Navigation;
using System.Diagnostics;
using System.Text.RegularExpressions;


namespace VideoBrowser
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>

    public partial class MainWindow : System.Windows.Window
    {
        Dictionary<string, CloseableTabItem> tabs = new Dictionary<string, CloseableTabItem>();
        VideoDataSource vs = new VideoDataSource();
        public MainWindow()
        {
            InitializeComponent();
            
            TabItemAll.Content = new SearchResult(TabItemAll, "ALL",vs);
            this.AddHandler(CloseableTabItem.CloseTabEvent, new RoutedEventHandler(this.CloseTab));

            textsearch.Focus();
        }

        private void CloseTab(object source, RoutedEventArgs args)
        {
            TabItem tabItem = args.Source as TabItem;
            if (tabItem != null)
            {
                TabControl tabControl = tabItem.Parent as TabControl;
                if (tabControl != null)
                {
                    tabs.Remove(tabs.Where(p => p.Value == tabItem).First().Key);
                    tabControl.Items.Remove(tabItem);
                }
            }
        }

        private void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e)
        {
            Process.Start(e.Uri.AbsoluteUri);
        }

        private void buttonsearch_Click(object sender, RoutedEventArgs e)
        {
            String strSearch = textsearch.Text.ToString().Trim();
            String strToLowerSearch = textsearch.Text.ToLowerInvariant().Trim();
            if (!string.IsNullOrEmpty(strSearch))
            {
                bool found = false;
                CloseableTabItem theTabItem = null;

                if (tabs.ContainsKey(strToLowerSearch))
                {
                    found = true;
                    theTabItem = (CloseableTabItem)tabs[strToLowerSearch];
                }

                if (found == false)
                {
                    theTabItem = new CloseableTabItem();
                    theTabItem.Header = "Loading...";
                    SearchResult sr = new SearchResult(theTabItem, strSearch, vs);
                    theTabItem.Content = sr;
                    TabContainer.Items.Add(theTabItem);
                    tabs.Add(strToLowerSearch , theTabItem);
                }
                theTabItem.Focus();
                textsearch.Text = "";
                textsearch.Focus();
            }
            else
            {
                textsearch.Text = "";
                textsearch.Focus();
            }
        }        
    }
}